<?php

function import($lib_name) {
    require "libs/$lib_name.lib.php";
}

function find_all_files($dir) 
{ 
    $root = scandir($dir); 
    foreach($root as $value) 
    { 
        if($value === '.' || $value === '..') {continue;} 
        if(is_file("$dir/$value")) {$result[]="$dir/$value";continue;} 
        foreach(find_all_files("$dir/$value") as $value) 
        { 
            $result[]=$value; 
        } 
    } 
    return $result; 
}
function save_logs($filename, $content) {
    $filename = LOG_PATH . '/' . $filename;
    $handle = @fopen($filename, "a");
    if (!$handle)
        $handle = @fopen($filename, "a");
    if (!$handle) {
        $response = new RequestResponse(500, "Cannot handler log file");
        $response->process();
    }
    fwrite($handle, "=============== " . gmdate("d/m/Y h:i:s A") . " ============\n" . $content . "\n FILE: " . @$_SERVER["REQUEST_URI"] . "\n");
    fclose($handle);
}

function replace_mq($text) {
    $text = str_replace("\'", "'", $text);
    $text = str_replace("'", "''", $text);
    return $text;
}

function str_sql_clean($string) {
    return '"' . addslashes(replace_mq($string)) . '"';
}

function get_host_url() {
    $pageURL = 'http';
    if ($_SERVER["HTTPS"] == "on") {
        $pageURL .= "s";
    }
    $pageURL .= "://";
    if ($_SERVER["SERVER_PORT"] != "80") {
        $pageURL .= $_SERVER["SERVER_NAME"] . ":" . $_SERVER["SERVER_PORT"];
    } else {
        $pageURL .= $_SERVER["SERVER_NAME"];
    }
    return $pageURL;
}

function get_request_uri() {
    return getHostUrl() . $_SERVER["REQUEST_URI"];
}

function get_request_method() {
    return $_SERVER['REQUEST_METHOD'];
}

function get_http_response_code($url) {
    $headers = get_headers($url);
    return substr($headers[0], 9, 3);
}
function echoArray($array){
   if(count($array) > 0){
      echo '<pre>';
      print_r($array);
      echo '</pre>';
   }else{
      echo 'Mảng rỗng';
   }
}
if (!function_exists('getallheaders')) {

    function getallheaders() {
        $headers = '';
        foreach ($_SERVER as $name => $value) {
            if (substr($name, 0, 5) == 'HTTP_') {
                $headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;
            }
        }
        return $headers;
    }

}
